package CtrLag;
import ModelLag.*;

/**
 * The DVDCtrl class is used to control the DVD-, DVDContainer- and Copy class by calling their methods
 * 
 * @author (Lackshan, Mikkel, Nick, patrick, Simon, Yousef) 
 * @project (Tema design)
 * @version (2014.11.04)
 */
public class DVDCtr
{
    private DVDContainer dCon;
    /**
     * Constructor for objects of class DVDCtrl
     */
    public DVDCtr(){
        dCon = DVDContainer.getInstance();
    }
    
    /**
     * Creates a new DVD
     * 
     * @param barcode the barcode of the DVD
     * @param title the title of the DVD
     * @param artist the DVD´s artist
     * @param publicationDate the dvd´s date of publish
     */
    public void createDVD(String barcode, String title, String artist, String publicationDate){
        DVD newDVD = new DVD(barcode.toLowerCase(), title.toLowerCase(), artist.toLowerCase(), publicationDate.toLowerCase());
        dCon.addDVD(newDVD);
    }
    
    /**
     * Finds a specific DVD based on barcode
     * 
     * @param barcode the barecode of the DVD
     * @return if found the requested DVD is returned
     */
    public DVD findDVD(String barcode){
        try{
            DVD d  = dCon.findDVD(barcode.toLowerCase());
            return d;
        }catch(Exception e){
            return null;
        }
    }
    
      /**
     * Finds a specific DVD based on serialNumber of a copy
     * 
     * @param barcode the barecode of the DVD
     * @return if found the requested DVD is returned
     */
    public DVD findDVDfromCopy(String serialNumber){
        try{
            DVD d  = dCon.findDVDfromCopy(serialNumber.toLowerCase());
            return d;
        }catch(Exception e){
            return null;
        }
    }
    
    /**
     * Updates a specific DVD´s informations
     * 
     * @param barcode the barcode of the DVD
     * @param newTitle the new title of the DVD
     * @param newArtist the DVD´s new artist
     * @param newPublicationDate the dvd´s new date of publish
     * 
     * @return returns true if the person was updated
     */
    public boolean updateDVD(String barcode, String newTitle, String newArtist, String newPublicationDate){
        DVD d = findDVD(barcode.toLowerCase());
        if(d != null){
            d.setTitle(newTitle.toLowerCase());
            d.setArtist(newArtist.toLowerCase());
            d.setPublicationDate(newPublicationDate.toLowerCase());
            return true;
        }
        return false;
    }
    
    /**
     * Deletes a specific DVD from the DVD Container
     * 
     * @param barcode the barcode of the DVD
     */
    public void deleteDVD(String barcode){
        dCon.deleteDVD(barcode.toLowerCase());
    }
    
    /**
     * Creats a Copy of an DVD
     * 
     * @param barcode, the barcode of the DVD which it's a copy of
     * @param serialNumber, the serial number of the copy
     * @param purchaseDate, the purchase date of the copy
     * @param purchasePrice, the purchase price of the copy
     */
    public void createCopy(String barcode, String serialNumber, String purchaseDate, double purchasePrice){
        DVD d = findDVD(barcode);
        if(d != null){
            Copy newCopy = new Copy(serialNumber.toLowerCase(), purchaseDate.toLowerCase(), purchasePrice);
            d.addCopy(newCopy);
        }
        
    }
    
    /**
     * Returns a copy if is found, else returns null
     * 
     * @param serialNumber the serial number of the copy
     * @return a copy object
     **/
    public Copy findCopy(String serialNumber){
        DVD d = findDVDfromCopy(serialNumber.toLowerCase());//Returns the dvd object where the Copy is located
        if(d != null){
            Copy c = d.findCopy(serialNumber.toLowerCase());
            return c;
        }
        return null;
    }
    
    /**
     * Deletes a copy from the system
     * 
     * @param serialNumber the serial number of the copy
     */
    public void deleteCopy(String serialNumber){
        DVD d = findDVDfromCopy(serialNumber.toLowerCase());//Returns the dvd object where the Copy is located
        d.deleteCopy(serialNumber.toLowerCase());
    }
        
}
